include("shared.lua")

function ENT:Draw()
        self.Entity:DrawModel()
end

function ENT:DrawGUI()
	local CrateMenu = vgui.Create("DFrame")
	CrateMenu:SetSize(268, 289)
	CrateMenu:Center()
	CrateMenu:SetVisible( true )
	CrateMenu:MakePopup( )
	CrateMenu:SetTitle("Crate")


	
	CrateMenu.Items = vgui.Create( "DPanelList", CrateMenu ) 
	CrateMenu.Items:EnableVerticalScrollbar( true ) 
 	CrateMenu.Items:EnableHorizontal( true ) 
 	CrateMenu.Items:SetPadding( 2 )
	CrateMenu.Items:StretchToParent(4, 27, 4, 4)

	
	
	for k, v in pairs(self.Entity.Items) do
		local ItemIcon = vgui.Create( "SpawnIcon", CrateMenu.Items) 
		ItemIcon:SetModel( v.Model )
		ItemIcon:SetToolTip( v.Name )
		CrateMenu.Items:AddItem( ItemIcon )
		ItemIcon.DoClick = function( ItemIcon )
		    local Menu = DermaMenu()
		    Menu:AddOption("Withdraw " .. v.Name, function()
				RunConsoleCommand( "RP_CrateWithdraw", self.Entity:EntIndex( ), v.Class)
				CrateMenu:Close()
			end )
		    Menu:AddOption("Cancel", function() end )
		    Menu:Open()
		end 
	end
end

local function UpdateItems(handle, id, encoded, decoded)
	decoded.Entity.Items = decoded.Items
	decoded.Entity:DrawGUI()
end
datastream.Hook( "CrateItems", UpdateItems )